﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController
    {
        [ChildActionOnly]
        public ActionResult SEXABP(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            if (!MWB.IsABPReady)
            {
                INFO = "请扫描有效的单号";
            }
            return PartialView(MWB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SEXABP(WBBSTModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                if (model.ABPAUF.Trim().Length < 11)
                {
                    return View();
                }
                MWB.ABPAUF = model.ABPAUF.Substring(0, 11); 
                QEXTSOURCE(model);
                if (!MWB.IsABPReady)
                {
                    INFO = "生产单号不正确";                   
                }
               
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP); 
        }
        public void QEXTSOURCE(WBBSTModel model)
        {

            MWB.IsABPReady = false;

            if (model.ABPAUF == null)
                return;
            if (model.ABPAUF.Trim().Length < 11)
            {
                MWB.IsABPReady = false;
                return;
            }
            MWB.TP = "0000";
            model.ABPAUF = model.ABPAUF.Trim().PadRight(11);
            PM["KRS"] = model.ABPAUF.Substring(0, 2);
            PM["LFD"] = model.ABPAUF.Substring(2, 6);
            PM["POS"] = model.ABPAUF.Substring(8, 3);
            SResult = VHP.ABP.LOADABTP(PM);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                MWB.TP = SResult.Tables[0].Rows[0]["WBBTP"].ToString();
                MWB.WHPCS.Add(row["WBBTP"].ToString(), row["PCS"].ToString() + "{"+ row["LG"].ToString()+"}");
            }  
            MWB.IsABPReady = true;
        }
    }
}
